package vip.xiaonuo.issued.impl.visitor;

import com.alibaba.fastjson.JSON;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import vip.xiaonuo.facedoor.modular.devicebase.entity.DeviceBase;
import vip.xiaonuo.facedoor.modular.memberdevice.constant.MemberDeviceConstant;
import vip.xiaonuo.facedoor.iservice.entity.PersonBaseParam;
import vip.xiaonuo.facedoor.iservice.entity.PersonBaseParamRet;
import vip.xiaonuo.facedoor.iservice.issulog.DeviceLogService;
import vip.xiaonuo.facedoor.modular.visitor.entity.Visitor;
import vip.xiaonuo.facedoor.modular.visitordevice.entity.VisitorDevice;
import vip.xiaonuo.facedoor.modular.visitordevice.service.VisitorDeviceService;
import vip.xiaonuo.facedoor.remote.iservice.adapter.VisitorReqAdapter;
import vip.xiaonuo.facedoor.iservice.IVisitorDealWithService;
import vip.xiaonuo.facedoor.rule.RuleConstant;

@Service(value = RuleConstant.DW+ RuleConstant.VISITOR_DEAL_WITH)
public class MqttVisitorDealWithServiceImpl implements IVisitorDealWithService {

  @Autowired
  private VisitorReqAdapter visitorReqAdapter;

  @Autowired
  private VisitorDeviceService visitorDeviceService;

  @Autowired
  private DeviceLogService logService;


  @Override
  public PersonBaseParamRet getBaseParam(PersonBaseParam personBasePerson) {
    return new PersonBaseParamRet().setUrl("http://" + personBasePerson.getDeviceBase().getCabinetIp() + ":" + personBasePerson.getDeviceBase().getCabinetPort());
  }

  @Override
  public void deleteException(VisitorDevice visitorDevice, Visitor visitor, DeviceBase deviceBase, Exception e) {
    visitorDevice.setFailReason(JSON.toJSONString(ExceptionUtils.getMessage(e)));
    visitorDevice.setStatus(MemberDeviceConstant.DELETE_FAIL);
    visitorDeviceService.updateById(visitorDevice);
    logService.addVisitorLog(visitor.getName(), deviceBase.getName(), ExceptionUtils.getMessage(e), FAIL, UPDATE);
  }

  @Override
  public void delete(VisitorDevice visitorDevice, Visitor visitor, PersonBaseParamRet baseParam, DeviceBase deviceBase) throws Exception {
    visitor.setMemberType(2);
    visitor.setMemberOrVisitorDeviceId(visitorDevice.getId());
    visitorReqAdapter.deletePerson(visitorDevice, visitorDevice.getDeviceId() + "");
  }

  @Override
  public void add(VisitorDevice visitorDevice, Visitor visitor, PersonBaseParamRet baseParam, DeviceBase deviceBase) throws Exception {
    visitor.setMemberType(2);
    visitor.setMemberOrVisitorDeviceId(visitorDevice.getId());
    visitorReqAdapter.addPerson(visitorDevice.getDeviceId() + "", visitor);
  }

  @Override
  public void addException(VisitorDevice visitorDevice, Visitor visitor, DeviceBase deviceBase, Exception e) {
    visitorDevice.setFailReason(JSON.toJSONString(ExceptionUtils.getMessage(e)));
    visitorDevice.setStatus(MemberDeviceConstant.ADD_FAIL);
    visitorDeviceService.updateById(visitorDevice);
    logService.addVisitorLog(visitor.getName(), deviceBase.getName(), ExceptionUtils.getMessage(e), FAIL, ADD);
  }

  @Override
  public void updateException(VisitorDevice visitorDevice, Visitor visitor, DeviceBase deviceBase, Exception e) {
    visitorDevice.setFailReason(JSON.toJSONString(ExceptionUtils.getMessage(e)));
    visitorDevice.setStatus(MemberDeviceConstant.UPDATE_FAIL);
    visitorDeviceService.updateById(visitorDevice);
    logService.addVisitorLog(visitor.getName(), deviceBase.getName(), ExceptionUtils.getMessage(e), FAIL, UPDATE);
  }

  @Override
  public void update(VisitorDevice visitorDevice, Visitor visitor, PersonBaseParamRet baseParam, DeviceBase deviceBase) throws Exception {
    visitor.setMemberType(2);
    visitor.setMemberOrVisitorDeviceId(visitorDevice.getId());
    visitorReqAdapter.updatePerson(visitorDevice.getDeviceId()+"", visitor);
  }

}
